/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.compomics.daubechiesfilter.statistics;

import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;

/**
 *
 * @author Kenneth
 */
public class ThresholdGenerator {
    /*The threshold
     (N being the signal length, σ being the noise variance) is well known in wavelet literature as the Universal threshold. 
     */

    public static double getThresHold(double[] intensities) {
        double[] tempIntensities = intensities.clone();
        double threshold = 0;
        double dev = 0;
        double avIntensity = 0;
        DescriptiveStatistics sumStats = new DescriptiveStatistics();

        //load summary
        for (double anIntent : tempIntensities) {
            sumStats.addValue(anIntent);
        }
        avIntensity = sumStats.getMean();
        dev = sumStats.getStandardDeviation();
        System.out.println("------BEFORE NOISE ISOLATION-------");
        System.out.println("Mean of frame : " + avIntensity);
        System.out.println("Stdev of frame : " + dev);
        System.out.println("Highest value in frame : " + sumStats.getMax());
        System.out.println("Lowest value in frame : " + sumStats.getMin());
        System.out.println("------AFTER NOISE ISOLATION-------");
        sumStats.clear();
        //remove all outliers?
        for (double anIntent : tempIntensities) {
            if (anIntent < (avIntensity + (dev)) && anIntent >= 0) {
                //Then this is NOT a positive outlier ---> only need noise
                sumStats.addValue(anIntent);
            }
        }
        //genuine noise deviation = 
        dev = sumStats.getStandardDeviation();
        long N = sumStats.getN();
        System.out.println("Mean of frame : " + sumStats.getMean());
        System.out.println("Stdev of frame : " + dev);
        System.out.println("Highest value in frame : " + sumStats.getMax());
        System.out.println("Lowest value in frame : " + sumStats.getMin());
        threshold = Math.abs(dev * Math.sqrt(2 * Math.log10(N)));

        return threshold;
    }
}
